package com.jintian.smart.platform.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.jintian.smart.base.api.security.provider.ILevel3ProtectConfigProvider;
import com.jintian.smart.base.api.system.constant.SystemSwaggerTagConst;
import com.jintian.smart.base.api.system.form.*;
import com.jintian.smart.base.api.system.view.EmployeeVO;
import com.jintian.smart.kernel.common.context.ContextHolder;
import com.jintian.smart.kernel.core.domain.ResponseDTO;
import com.jintian.smart.kernel.core.support.ApiDecrypt;
import com.jintian.smart.platform.system.service.EmployeeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
import org.beetl.sql.core.page.PageResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工
 */
@RestController
@Tag(name = SystemSwaggerTagConst.SYSTEM_EMPLOYEE)
@RequestMapping("/sys/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    @DubboReference
    private ILevel3ProtectConfigProvider level3ProtectConfigProvider;

    @PostMapping("/query")
    @Operation(summary = "员工管理查询")
    public ResponseDTO<PageResult<EmployeeVO>> query(@Valid @RequestBody EmployeeQueryForm query) {
        return employeeService.queryEmployee(query);
    }

    @Operation(summary = "添加员工(返回添加员工的密码)")
    @PostMapping("/add")
    @SaCheckPermission("system:employee:add")
    public ResponseDTO<String> addEmployee(@Valid @RequestBody EmployeeAddForm employeeAddForm) {
        return employeeService.addEmployee(employeeAddForm);
    }

    @Operation(summary = "更新员工")
    @PostMapping("/update")
    @SaCheckPermission("system:employee:update")
    public ResponseDTO<String> updateEmployee(@Valid @RequestBody EmployeeUpdateForm employeeUpdateForm) {
        return employeeService.updateEmployee(employeeUpdateForm);
    }

    @Operation(summary = "更新员工个人中心信息")
    @PostMapping("/update/center")
    public ResponseDTO<String> updateCenter(@Valid @RequestBody EmployeeUpdateCenterForm updateCenterForm) {
        updateCenterForm.setEmployeeId(ContextHolder.getUserIdLong());
        return employeeService.updateCenter(updateCenterForm);
    }

    @Operation(summary = "更新登录人头像")
    @PostMapping("/update/avatar")
    public ResponseDTO<String> updateAvatar(@Valid @RequestBody EmployeeUpdateAvatarForm employeeUpdateAvatarForm) {
        employeeUpdateAvatarForm.setEmployeeId(ContextHolder.getUserIdLong());
        return employeeService.updateAvatar(employeeUpdateAvatarForm);
    }

    @Operation(summary = "更新员工禁用/启用状态")
    @GetMapping("/update/disabled/{employeeId}")
    @SaCheckPermission("system:employee:disabled")
    public ResponseDTO<String> updateDisableFlag(@PathVariable Long employeeId) {
        return employeeService.updateDisableFlag(employeeId);
    }

    @Operation(summary = "批量删除员工")
    @PostMapping("/update/batch/delete")
    @SaCheckPermission("system:employee:delete")
    public ResponseDTO<String> batchUpdateDeleteFlag(@RequestBody List<Long> employeeIdList) {
        return employeeService.batchUpdateDeleteFlag(employeeIdList);
    }

    @Operation(summary = "批量调整员工部门")
    @PostMapping("/update/batch/department")
    @SaCheckPermission("system:employee:department:update")
    public ResponseDTO<String> batchUpdateDepartment(@Valid @RequestBody EmployeeBatchUpdateDepartmentForm batchUpdateDepartmentForm) {
        return employeeService.batchUpdateDepartment(batchUpdateDepartmentForm);
    }

    @Operation(summary = "修改密码")
    @PostMapping("/update/password")
    @ApiDecrypt
    public ResponseDTO<String> updatePassword(@Valid @RequestBody EmployeeUpdatePasswordForm updatePasswordForm) {
        updatePasswordForm.setEmployeeId(ContextHolder.getUserIdLong());
        return employeeService.updatePassword(ContextHolder.getUser(), updatePasswordForm);
    }

    @Operation(summary = "获取密码复杂度")
    @GetMapping("/getPasswordComplexityEnabled")
    @ApiDecrypt
    public ResponseDTO<Boolean> getPasswordComplexityEnabled() {
        return ResponseDTO.ok(level3ProtectConfigProvider.isPasswordComplexityEnabled());
    }

    @Operation(summary = "重置员工密码")
    @GetMapping("/update/password/reset/{employeeId}")
    @SaCheckPermission("system:employee:password:reset")
    public ResponseDTO<String> resetPassword(@PathVariable Long employeeId) {
        return employeeService.resetPassword(employeeId);
    }

    @Operation(summary = "查询员工-根据部门id")
    @GetMapping("/getAllEmployeeByDepartmentId/{departmentId}")
    public ResponseDTO<List<EmployeeVO>> getAllEmployeeByDepartmentId(@PathVariable Long departmentId) {
        return employeeService.getAllEmployeeByDepartmentId(departmentId);
    }

    @Operation(summary = "查询所有员工")
    @GetMapping("/queryAll")
    public ResponseDTO<List<EmployeeVO>> queryAllEmployee(@RequestParam(value = "disabledFlag", required = false) Integer disabledFlag) {
        return employeeService.queryAllEmployee(disabledFlag);
    }
}
